package com.openbravo.data.loader;

import com.openbravo.format.Formats;

public class TableDefinition
{
  private Session m_s;
 public   String tablename;
  private String[] fieldname;
  private String[] fieldtran;
  private Datas[] fielddata;
  private Formats[] fieldformat;
  private int[] idinx;

  public TableDefinition(Session paramSession, String paramString, String[] paramArrayOfString1, String[] paramArrayOfString2, Datas[] paramArrayOfDatas, Formats[] paramArrayOfFormats, int[] paramArrayOfInt)
  {
    this.m_s = paramSession;
    this.tablename = paramString;
    this.fieldname = paramArrayOfString1;
    this.fieldtran = paramArrayOfString2;
    this.fielddata = paramArrayOfDatas;
    this.fieldformat = paramArrayOfFormats;
    this.idinx = paramArrayOfInt;
  }

  public TableDefinition(Session paramSession, String paramString, String[] paramArrayOfString, Datas[] paramArrayOfDatas, Formats[] paramArrayOfFormats, int[] paramArrayOfInt)
  {
    this(paramSession, paramString, paramArrayOfString, paramArrayOfString, paramArrayOfDatas, paramArrayOfFormats, paramArrayOfInt);
  }

  public String getTableName()
  {
    return this.tablename;
  }

  public String[] getFields()
  {
    return this.fieldname;
  }

  public Vectorer getVectorerBasic(int[] paramArrayOfInt)
  {
    return new VectorerBasic(this.fieldtran, this.fieldformat, paramArrayOfInt);
  }

  public IRenderString getRenderStringBasic(int[] paramArrayOfInt)
  {
    return new RenderStringBasic(this.fieldformat, paramArrayOfInt);
  }

  public ComparatorCreator getComparatorCreator(int[] paramArrayOfInt)
  {
    return new ComparatorCreatorBasic(this.fieldtran, this.fielddata, paramArrayOfInt);
  }

  public IKeyGetter getKeyGetterBasic()
  {
    if (this.idinx.length == 1)
      return new KeyGetterFirst(this.idinx);
    return new KeyGetterBasic(this.idinx);
  }

  public SerializerRead getSerializerReadBasic()
  {
    return new SerializerReadBasic(this.fielddata);
  }

  public SerializerWrite getSerializerInsertBasic(int[] paramArrayOfInt)
  {
    return new SerializerWriteBasicExt(this.fielddata, paramArrayOfInt);
  }

  public SerializerWrite getSerializerDeleteBasic()
  {
    return new SerializerWriteBasicExt(this.fielddata, this.idinx);
  }

  public SerializerWrite getSerializerUpdateBasic(int[] paramArrayOfInt)
  {
    int[] arrayOfInt = new int[paramArrayOfInt.length + this.idinx.length];
    for (int i = 0; i < paramArrayOfInt.length; i++)
      arrayOfInt[i] = paramArrayOfInt[i];
    for (int i = 0; i < this.idinx.length; i++)
      arrayOfInt[(i + paramArrayOfInt.length)] = this.idinx[i];
    return new SerializerWriteBasicExt(this.fielddata, arrayOfInt);
  }

  public SentenceList getListSentence()
  {
    return getListSentence(getSerializerReadBasic());
  }

  public SentenceList getListSentence(SerializerRead paramSerializerRead)
  {
    return new PreparedSentence(this.m_s, getListSQL(), null, paramSerializerRead);
  }

  public String getListSQL()
  {
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append("select ");
    for (int i = 0; i < this.fieldname.length; i++)
    {
      if (i > 0)
        localStringBuffer.append(", ");
      localStringBuffer.append(this.fieldname[i]);
    }
    localStringBuffer.append(" from ");
    localStringBuffer.append(this.tablename);
    return localStringBuffer.toString();
  }

  public SentenceExec getDeleteSentence()
  {
    return getDeleteSentence(getSerializerDeleteBasic());
  }

  public SentenceExec getDeleteSentence(SerializerWrite paramSerializerWrite)
  {
    return new PreparedSentence(this.m_s, getDeleteSQL(), paramSerializerWrite, null);
  }

  public String getDeleteSQL()
  {
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append("delete from ");
    localStringBuffer.append(this.tablename);
    for (int i = 0; i < this.idinx.length; i++)
    {
      localStringBuffer.append(i == 0 ? " where " : " and ");
      localStringBuffer.append(this.fieldname[this.idinx[i]]);
      localStringBuffer.append(" = ?");
    }
    return localStringBuffer.toString();
  }

  public SentenceExec getInsertSentence()
  {
    return getInsertSentence(getAllFields());
  }

  public SentenceExec getInsertSentence(int[] paramArrayOfInt)
  {
    return new PreparedSentence(this.m_s, getInsertSQL(paramArrayOfInt), getSerializerInsertBasic(paramArrayOfInt), null);
  }
  public static ThreadLocal<String> tablenameQ1=new ThreadLocal<String>();
  private String getInsertSQL(int[] paramArrayOfInt)
  {
	  if(this.tablename!=null)
	  tablenameQ1.set(this.tablename);
    StringBuffer localStringBuffer1 = new StringBuffer();
    StringBuffer localStringBuffer2 = new StringBuffer();
    localStringBuffer1.append("insert into ");
    localStringBuffer1.append(this.tablename);
    localStringBuffer1.append(" (");
    for (int i = 0; i < paramArrayOfInt.length; i++)
    {
      if (i > 0)
      {
        localStringBuffer1.append(", ");
        localStringBuffer2.append(", ");
      }
      localStringBuffer1.append(this.fieldname[paramArrayOfInt[i]]);
      localStringBuffer2.append("?");
    }
    localStringBuffer1.append(") values (");
    localStringBuffer1.append(localStringBuffer2.toString());
    localStringBuffer1.append(")");
    return localStringBuffer1.toString();
  }

  private int[] getAllFields()
  {
    int[] arrayOfInt = new int[this.fieldname.length];
    for (int i = 0; i < this.fieldname.length; i++)
      arrayOfInt[i] = i;
    return arrayOfInt;
  }

  public SentenceExec getUpdateSentence()
  {
    return getUpdateSentence(getAllFields());
  }

  public SentenceExec getUpdateSentence(int[] paramArrayOfInt)
  {
    return new PreparedSentence(this.m_s, getUpdateSQL(paramArrayOfInt), getSerializerUpdateBasic(paramArrayOfInt), null);
  }

  private String getUpdateSQL(int[] paramArrayOfInt)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append("update ");
    localStringBuffer.append(this.tablename);
    localStringBuffer.append(" set ");
    for (int i = 0; i < paramArrayOfInt.length; i++)
    {
      if (i > 0)
        localStringBuffer.append(", ");
      localStringBuffer.append(this.fieldname[paramArrayOfInt[i]]);
      localStringBuffer.append(" = ?");
    }
    for (int i = 0; i < this.idinx.length; i++)
    {
      localStringBuffer.append(i == 0 ? " where " : " and ");
      localStringBuffer.append(this.fieldname[this.idinx[i]]);
      localStringBuffer.append(" = ?");
    }
    return localStringBuffer.toString();
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.data.loader.TableDefinition
 * JD-Core Version:    0.6.0
 */